package com.haixiaoke.saas.enterprise.house.controller;

import java.util.List;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.haixiaoke.saas.common.annotation.TenantScope;
import com.haixiaoke.saas.common.constant.HouseConstants;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.common.exception.ServiceException;
import com.haixiaoke.saas.common.tenant.TenantContext;
import com.haixiaoke.saas.house.domain.HousePicture;
import com.haixiaoke.saas.house.service.IHousePictureService;
import com.haixiaoke.saas.system.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 房屋相册信息Controller
 *
 * @author xiaolong
 * @date 2024-10-22
 */
@RestController
@RequestMapping("/enterprise/house/picture")
public class EnterpriseHousePictureController extends BaseController {
    @Autowired
    private IHousePictureService housePictureService;

    /**
     * 查询房屋相册信息列表
     */
    @PreAuthorize("@ss.hasPermi('enterprise:picture:list')")
    @GetMapping("/list")
    @TenantScope
    public TableDataInfo list(HousePicture housePicture) {
        startPage();
        housePicture.setTenantId(TenantContext.getTenantId());
        List<HousePicture> list = housePictureService.packagingHousePictureList(housePicture);
        return getDataTable(list);
    }


    /**
     * 获取房屋相册信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('enterprise:picture:getInfo')")
    @GetMapping(value = "/{housePictureId}")
    public AjaxResult getInfo(@PathVariable("housePictureId") Long housePictureId) {
        return success(housePictureService.selectHousePictureByHousePictureId(housePictureId));
    }

    /**
     * 根据分组标识获取房屋相册信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('enterprise:picture:query')")
    @GetMapping(value = "/info")
    @TenantScope
    public AjaxResult getInfo(HousePicture housePicture) {
        housePicture.setTenantId(TenantContext.getTenantId());
        return success(housePictureService.selectHousePictureList(housePicture));
    }


    /**
     * 新增日常检查
     */
    @PreAuthorize("@ss.hasPermi('enterprise:picture:add')")
    @PostMapping
    @TenantScope
    public AjaxResult add(@RequestBody HousePicture housePicture) {
        if (StrUtil.isBlank(housePicture.getPictureSet())) {
            throw new ServiceException("请上传照片！");
        }
        if (housePicture.getPictureSet().split(",").length > 6) {
            throw new ServiceException("照片不能超过6张！");
        }
        housePicture.setHousePictureType(HouseConstants.HOUSE_PICTURE_DAILY);
        housePicture.setUploadUser(getUsername());
        housePicture.setUploadUserId(getUserId());
        housePicture.setTenantId(TenantContext.getTenantId());
        housePicture.setPictureNumber(housePicture.getPictureSet().split(",").length);
        // 生成分组编号
        String packetIdentification = RandomUtil.randomStringWithoutStr(10, "0123456789");
        housePicture.setPacketIdentification(packetIdentification);
        return toAjax(housePictureService.insertHousePicture(housePicture));
    }

    /**
     * 根据用户id获取房屋相册列表
     */
    @PreAuthorize("@ss.hasPermi('enterprise:picture:appletList')")
    @GetMapping(value = "/appletList")
    @TenantScope
    public TableDataInfo appletList(HousePicture housePicture) {
        startPage();
        housePicture.setTenantId(TenantContext.getTenantId());
        housePicture.setUploadUserId(getUserId());
        housePicture.setHousePictureType(HouseConstants.HOUSE_PICTURE_DAILY);
        return getDataTable(housePictureService.selectHousePictureList(housePicture));
    }

}
